<?php

/**
 * This is the model class for table "{{user}}".
 *
 * The followings are the available columns in table '{{user}}':
 * @property string $id
 * @property string $username
 * @property string $display_name
 * @property string $password
 * @property string $email
 * @property integer $status
 * @property integer $time_ban
 * @property integer $created_time
 * @property integer $updated_time
 * @property integer $recent_login
 * @property string $user_activation_key
 * @property integer $confirmed
 * @property string $gender
 * @property string $location
 * @property integer $birthday_month
 * @property integer $birthday_day
 * @property integer $birthday_year
 * @property string $avatar
 * @property integer $email_site_news
 * @property integer $email_search_alert
 * @property string $email_recover_key
 * @property string $former_display_name
 */
class User extends CActiveRecord
{
    public $birthday;
    public $repass;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{user}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, display_name, password, email', 'required'),
			array('repass', 'required','on'=>'insert'),
			array('status, created_time, updated_time, birthday_day, birthday_month, birthday_year, recent_login, confirmed, email_site_news, email_search_alert', 'numerical', 'integerOnly'=>true),
			array('username, password, email', 'length', 'max'=>128),
			array('display_name, user_activation_key, avatar, email_recover_key, former_display_name', 'length', 'max'=>255),
			array('gender', 'length', 'max'=>10),
			array('location', 'length', 'max'=>100),

            //Custom
            array('repass','compare','compareAttribute' => 'password','operator'=>'==','on'=>'insert'),
            array('email','email'),
            array('username, display_name, email','unique'),
            array('username','length','min'=>5),
            array('display_name','length','min'=>3),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, username, display_name, password, email, status,time_ban, created_time, updated_time, recent_login, user_activation_key, confirmed, gender, location, birthday_month, birthday_day, birthday_year, birthday, avatar, email_site_news, email_search_alert, email_recover_key, former_display_name', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => t('ID','userModel'),
			'username' => t('User Name','userModel'),
			'display_name' => t('Display name','userModel'),
			'password' => t('Password','userModel'),
			'email' => t('Email','userModel'),
			'status' => t('Status','userModel'),
			'time_ban' => t('Banned Time','userModel'),
			'created_time' => t('Create time','userModel'),
			'updated_time' => t('Updated Time','userModel'),
			'recent_login' => t('Recent Login','userModel'),
			'user_activation_key' => t('User Activation Key','userModel'),
			'confirmed' => t('Confirmed','userModel'),
			'gender' => t('Gender','userModel'),
			'location' => t('Location','userModel'),
			'birthday_month' => t('Birthday Month','userModel'),
			'birthday_day' => t('Birthday Day','userModel'),
			'birthday_year' => t('Birthday Year','userModel'),
			'avatar' => t('Avatar','userModel'),
			'email_site_news' => t('Email Site News','userModel'),
			'email_search_alert' => t('Email Search Alert','userModel'),
			'email_recover_key' => t('Email Recover Key','userModel'),
			'former_display_name' => t('Former Display Name','userModel'),
			'birthday' => t('Birthday','userModel'),
            'repass' => t('Confirm Password','userModel'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('display_name',$this->display_name,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('status',$this->filterStatus($this->status));
		$criteria->compare('time_ban',$this->time_ban);
		$criteria->compare('created_time',$this->created_time);
		$criteria->compare('updated_time',$this->updated_time);
		$criteria->compare('recent_login',$this->recent_login);
		$criteria->compare('user_activation_key',$this->user_activation_key,true);
		$criteria->compare('confirmed',$this->confirmed);
		$criteria->compare('gender',$this->gender,true);
		$criteria->compare('location',$this->location,true);
		$criteria->compare('birthday_month',$this->birthday_month,true);
		$criteria->compare('birthday_day',$this->birthday_day,true);
		$criteria->compare('birthday_year',$this->birthday_year,true);
		$criteria->compare('avatar',$this->avatar,true);
		$criteria->compare('email_site_news',$this->email_site_news);
		$criteria->compare('email_search_alert',$this->email_search_alert);
		$criteria->compare('email_recover_key',$this->email_recover_key,true);
		$criteria->compare('former_display_name',$this->former_display_name,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    public function rederStatus($data,$row){
        // $data ... the current row data
        // $row ... the row index
        $status = $data['status'];
        switch ($status) {
            case USER_ACTIVE :
                return t('Active');
                break;
            case USER_DEACTIVATE :
                return t('Deactivate');
                break;
            case USER_BANNED :
                return t('Banned');
                break;
        }

    }
    public function filterStatus($status){
        if(is_null($status)) return null;
        if(is_numeric($status)) return $status;
        switch ($status) {
            case t('Active') :
                return USER_ACTIVE;
                break;
            case t('Deactivate') :
                return USER_DEACTIVATE;
                break;
            case t('Banned') :
                return USER_BANNED;
                break;
            default :
                return null;
        }
    }
    public function beforeSave() {
        if($this->isNewRecord) {
            $this->password = $this->encrypt($this->password);
            $this->created_time = time();
        } else {
            $this->updated_time = time();
        }
        return parent::beforeSave();
    }
    public function encrypt($value)
    {
        $enc = NEW bCrypt();
        return $enc->hash($value);
    }
}